<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
	body, html,#allmap,p{width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=PFtCgXpjEjj1CmlfE9IAe4VY6fE44aPG"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
	<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
	<title>点聚合</title>
	
</head>
<body>
	<div id="allmap"></div>
	
	<script type="text/javascript">
	// 百度地图API功能
	var mapobj = new BMap.Map("allmap",{enableMapClick:false});
	mapobj.centerAndZoom(new BMap.Point(116.404, 39.915), 4);
	var markers = [];
	mapobj.enableScrollWheelZoom();//允许地图进行缩放
	mapobj.disableDoubleClickZoom();//禁止双击放大
	  //左上角，添加默认缩放平移控件
	mapobj.addControl(new BMap.NavigationControl()); 
	
   function ComplexCustomOverlay(point, text, num ){
      this._point = point;
      this._text = text;
      this._num = num;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
      this._map = map;
      var div = this._div = document.createElement("div");
      div.style.position = "absolute";
      div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
      div.style.backgroundColor = "#4AB5E9";
      div.style.color = "white";
      div.style.height = "53px";
      div.style.paddingTop = "32px";
      div.style.width = "85px";
      div.style.lineHeight = "16px";
      div.style.fontSize = "12px";
      div.style.borderRadius = "50%";
      div.style.textAlign = "center";
 
    
      var b = this._p = document.createElement("b");
      div.appendChild(b);
      b.appendChild(document.createTextNode(this._text));      
      var that = this;

     
      var p = this._p = document.createElement("p");
      div.appendChild(p);
      p.appendChild(document.createTextNode(this._num));      

      var arrow = this._arrow = document.createElement("div");
      arrow.style.position = "absolute";
      arrow.style.width = "11px";
      arrow.style.height = "10px";
      arrow.style.top = "22px";
      arrow.style.left = "10px";
      arrow.style.overflow = "hidden";
      div.appendChild(arrow);
     
      div.onmouseover = function(){
        this.style.backgroundColor = "#FD8330";
        this.style.borderColor = "#0000ff";
        this.getElementsByTagName("b")[0].innerHTML = that._text;
        this.getElementsByTagName("p")[0].innerHTML = that._num;
        arrow.style.backgroundPosition = "0px -20px";
      }

      div.onmouseout = function(){
        this.style.backgroundColor = "#4AB5E9";
        this.style.borderColor = "#BC3B3A";
        this.getElementsByTagName("b")[0].innerHTML = that._text;
        this.getElementsByTagName("p")[0].innerHTML = that._num;
        arrow.style.backgroundPosition = "0px 0px";
      }
      div.addEventListener("click", function(e) {//点击事件绑定
           console.log(mapobj.getZoom());
//           mapobj.setZoom(7);
//   	             console.log(e);
//            document.getElementsByTagName('div').style.display='none';
          })
      mapobj.getPanes().labelPane.appendChild(div);
      
      return div;
    }
    ComplexCustomOverlay.prototype.draw = function(){
      var map = this._map;
      var pixel = map.pointToOverlayPixel(this._point);
      this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
      this._div.style.top  = pixel.y - 30 + "px";
    }

    	    
   if(mapobj.getZoom()==5){}
	    getData();
	   
   function  getData(){
    	$.ajax({
    		url:"{{route('map')}}",
    		type:'post',
    		dataType:'json',
    		data:{},
    		success:function(data){
    			  var pointArray = new Array();
    				 for (var i = 0; i < data.length; i++) {
    					var str=data[i].position.position;
    					var markerPosition = str.split(",");
    	                pt = new BMap.Point(markerPosition[0],markerPosition[1]);
    	                var marker=new BMap.Marker(pt);
    	                var myCompOverlay = new ComplexCustomOverlay(pt, data[i].shortform,i+'套');
    	                mapobj.addOverlay(myCompOverlay);
    	                pointArray[i] = new BMap.Point(markerPosition[0],markerPosition[1]);
    // 	                mapobj.centerAndZoom(pt, 15); 
    	            	
    	                marker.addEventListener("click",function(e){
    	                	   console.log(e.target.getPosition())
    		                });
    
    			 }
    			
    			mapobj.setViewport(pointArray);
    		},
    		Error:function(){
    			
    		}
	   });
   }
     
    //最简单的用法，生成一个marker数组，然后调用markerClusterer类即可。
	var markerClusterer = new BMapLib.MarkerClusterer(mapobj, {markers:markers});
	// 复杂的自定义覆盖物
        
   


























	
</script>
</body>
</html>

